package com.share.finance.risk.rules.web.exe;

import com.alibaba.fastjson.JSONObject;
import com.share.common.common.Result;
import com.share.finance.risk.rules.service.exe.TranslateService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class TranslateController {
    @Autowired
    private TranslateService translateService;

    //翻译文件
    @ApiOperation(value = "翻译一个策略")
    @RequestMapping(value = "/translate/strategy")
    public Result translateStrategy(JSONObject req) {
        if (req == null) {
            return Result.failure("参数不能为空");
        }
        String code = translateService.translateStrategy(req);
        return Result.success(code);
    }

    @ApiOperation(value = "翻译工程版本")
    @RequestMapping(value = "/translate/flow")
    public Result translateFlow(String strategyIds) {
        if (StringUtils.isEmpty(strategyIds)) {
            return Result.failure("strategyIds不能为空");
        }
        List<Long> list = Arrays.stream(strategyIds.split(",")).filter(e -> org.apache.commons.lang3.StringUtils.isNotBlank(e))
                .map(e -> Long.parseLong(e)).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(list)) {
            return Result.failure("strategyIds填写异常");
        }
        String code = translateService.translateFlow(list);
        return Result.success(code);
    }
}
